package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopMeasuringUnit;
import com.hyyt.shopjsp.product.service.IShopMeasuringUnitService;
import com.hyyt.shopjsp.product.service.IShopProductMeasuringUnitService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/7/31.
 */

@Controller
@RequestMapping("/back/productMeasuringUnit")
public class ProductMeasuringUnitController extends BaseController{

    private Logger logger = Logger.getLogger(ProductMeasuringUnitController.class);
    private String basePath = "/back/shop/product/shopProductType/productMeasuringUnit/";

    @Resource
    private IShopMeasuringUnitService shopMeasuringUnitService;
    @Resource
    private IShopProductMeasuringUnitService shopProductMeasuringUnitService;

    /**
     * 根据商品分类ID显示已添加的计量单位
     * @param productTypeId 商品分类id
     * @param page 分页
     * @param name 计量单位名称
     * @param response 返回结果集
     */
    @RequestMapping(value = "listSelectedMeasuringUnitByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void listSelectedMeasuringUnitByProductTypeId(Integer productTypeId, String name, Page page, HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(name.trim())){
            params.put("name",name);
        }
        if(Utils.objectIsNotEmpty(productTypeId)){
            params.put("productTypeId",productTypeId);
        }
        int totalRecordCount = shopMeasuringUnitService.countSelectedMeasuringUnitByProductTypeId(params);
        params.put("sortColumns", "measuringUnitId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<Map<String,Object>> shopMeasuringUnitList = shopMeasuringUnitService.querySelectedMeasuringUnitByProductTypeId(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopMeasuringUnitList);
        ResultMsg.create(params,response);
    }

    /**
     * 根据商品分类ID显示未添加的计量单位
     * @param productTypeId 商品分类id
     * @param page 分页
     * @param name 计量单位名称
     * @param response 返回结果集
     */
    @RequestMapping(value = "listUnselectedProductMeasuringUnitByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void listUnselectedProductMeasuringUnitByProductTypeId(String name,Integer productTypeId,Page page, HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(productTypeId)){
            params.put("productTypeId",productTypeId);
        }
        if(Utils.objectIsNotEmpty(name)){
            params.put("name",name.trim());
        }
        int totalRecordCount = shopMeasuringUnitService.countUnselectedMeasuringUnitByProductTypeId(params);
        params.put("sortColumns", "measuringUnitId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<ShopMeasuringUnit> shopMeasuringUnitList = shopMeasuringUnitService.queryUnselectedMeasuringUnitByProductTypeId(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopMeasuringUnitList);
        ResultMsg.create(params,response);
    }

    /**
     * 添加新的计量单位给商品分类
     * @param measuringUnitIds 计量单位id
     * @param productTypeId 商品分类id
     * @return boolean 返回布尔值
     */
    @RequestMapping(value = "saveSelectedProductMeasuringUnit",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveSelectedProductMeasuringUnit(Integer productTypeId, String measuringUnitIds) {
        if (Utils.objectIsNotEmpty(productTypeId) && Utils.objectIsNotEmpty(measuringUnitIds)) {
            shopProductMeasuringUnitService.saveShopMeasuringUnit(productTypeId,measuringUnitIds);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false, "保存失败，请重新操作");
        }
    }

    /**
     * 删除商品分类下的计量单位
     * @param productTypeId 商品分类id
     * @param measuringUnitId 计量单位id
     * @return boolean 返回布尔值
     */
    @RequestMapping(value = "deleteProductMeasuringUnit",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteProductMeasuringUnit(Integer productTypeId,Integer measuringUnitId){
        if(Utils.objectIsNotEmpty(productTypeId) && Utils.objectIsNotEmpty(measuringUnitId)){
            shopProductMeasuringUnitService.deleteMeasuringUnitByProductTypeIdAndMeasuringUnitId(productTypeId,measuringUnitId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false, "删除失败，请重新操作");
        }
    }

}
